import logging

from django.shortcuts import get_object_or_404, redirect
from django.forms import ModelForm
from django.views.decorators.cache import cache_page
from django.db.models import Q

from brispon.util import BaseSearchForm, paginate, inc_paginate, search
from brispon.util.decorators import render, render_to, ajax_request
from brispon.models import Store, Category

class StoreForm(BaseSearchForm):
    class Meta:
        base_qs = Store.objects.filter(Q(ad_type='BASIC'), ~Q(store_type='TEST'))
        search_fields = ('searchable',)
        default_fields = {}

#@cache_page(60 * 60)
@render_to('d/store/list.html')
def list(request):
    if request.is_mobile:
        return redirect('brispon.views.store.mlist')
    search_form = StoreForm(request.GET or None)
    if not request.GET:
        return {'objects': Store.search_objects.get_promotion(),
                'search_form': search_form,
                }
    return paginate(request.GET, search_form=search_form)

#@cache_page(60 * 60)
@render_to('d/store/inc_list.html')
def inc_list(request):
    store_form = StoreForm(request.GET or None)
    return inc_paginate(request.GET, search_form=store_form)

@render('d/store/detail.html')
def detail(request, name):
    if request.is_mobile:
        return redirect('brispon.views.store.mdetail', name)
    store = get_object_or_404(Store, name=name)
    my_favorite = store.is_favorite(request.user or None)
    if request.is_ajax():
        return like_ajax(request, store)
    return {'store': store,
            'my_favorite': my_favorite}

class StoreMapForm(BaseSearchForm):
    class Meta:
        base_qs = Store.objects.exclude(Q(addr__isnull=True)|Q(addr__exact=''))
        search_fields = ('searchable',)
        default_fields = {}
        
@cache_page(60 * 60)
@render_to('d/store/map.html')
def stores_map(request):
    store_form = StoreMapForm(request.GET or None)
    latitude = request.GET.get('lat', None)
    longitude = request.GET.get('long', None)
    if latitude and latitude:
        stores = Store.search_objects.get_near_stores_by_coordi(float(latitude), float(longitude))
    else:
        stores = search(request.GET, search_form=store_form)
    categories = Category.objects.all()
    return{'stores':stores,
           'categories': categories,
           'search_form': store_form}

## web test
@render_to('d/store/list_test.html')
def list_test(request):
    if request.is_mobile:
        return redirect('brispon.views.store.mlist_test')
    stores = Store.objects.filter(store_type='TEST')
    return {'stores':stores}

## mobile
@render_to('m/store/list.html')
def mlist(request):
    if not request.is_mobile:
        return redirect('brispon.views.store.list')
    store_form = StoreForm(request.GET or None)
    return paginate(request.GET, search_form=store_form, unit_page=12)

@render_to('m/store/inc_list.html')
def minc_list(request):
    get_page = request.GET['gp']
    stores = Store.objects.all()
    objects = inc_paginate(stores, get_page)
    return {'objects':objects}

@render_to('m/store/map.html')
def mstores_map(request):
    latitude = request.GET.get('lat', None)
    longitude = request.GET.get('long', None)
    if latitude and latitude:
        stores = Store.search_objects.get_near_stores_by_coordi(float(latitude), float(longitude))
    else:
        stores = Store.objects.all()
    categories = Category.objects.all()
    return{'stores':stores,
           'categories': categories,}
    
def like_ajax(request, store):
    result = {'success': True,
              'msg': ''
              }
    if request.user.is_anonymous():
        result['success'] = False
        result['msg'] = 'Log in firstly'
        return {'result': result}
        
    result['success'] = store.liked_by(request.user or None)
    result['msg'] = 'Success'
    return {'result':result}
    
@render_to('m/store/detail.html')
def mdetail(request, name):
    if not request.is_mobile:
        return redirect('brispon.views.store.detail', name)
    store = get_object_or_404(Store, name=name)
    return {'store':store}

## mobile test
@render_to('m/store/list.html')
def mlist_test(request):
    if not request.is_mobile:
        return redirect('brispon.views.store.list_test')
    store_form = StoreForm(request.GET or None)
    store_form.Meta.base_qs = Store.objects.filter(store_type='TEST')
    return paginate(request.GET, search_form=store_form, unit_page=12)